package com.dds.web.controller.system;

import java.util.List;
import java.util.Map;

import com.dds.common.core.domain.model.LoginUser;
import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import com.dds.system.domain.sys.SysDic;
import com.dds.system.domain.sys.SysDicCategory;
import com.dds.system.service.sys.ISysDicCategoryService;
import com.dds.system.service.sys.ISysDicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.common.utils.poi.ExcelUtil;
import com.dds.common.core.page.TableDataInfo;

/**
 * 数据字典分类Controller
 * 
 * @author gaolianjie
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/system/category")
public class SysDicCategoryController extends BaseController
{
    @Autowired
    private ISysDicCategoryService sysDicCategoryService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysDicService sysDicService;

    /**
     * 查询数据字典分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDicCategory sysDicCategory)
    {
        startPage();
        List<SysDicCategory> list = sysDicCategoryService.selectSysDicCategoryList(sysDicCategory);
        return getDataTable(list);
    }

    /**
     * 获取数据字典分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return AjaxResult.success(sysDicCategoryService.selectSysDicCategoryById(categoryId));
    }

    /**
     * 新增数据字典分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "数据字典分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDicCategory sysDicCategory)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();

        return toAjax(sysDicCategoryService.insertSysDicCategory(sysDicCategory, userId));
    }

    /**
     * 修改数据字典分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "数据字典分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDicCategory sysDicCategory)
    {
        return toAjax(sysDicCategoryService.updateSysDicCategory(sysDicCategory));
    }

    /**
     * 删除数据字典分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "数据字典分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(sysDicCategoryService.deleteSysDicCategoryByIds(categoryIds));
    }

    /**
     * 获取子分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:child:list')")
    @GetMapping("/childList/{categoryId}")
    public AjaxResult list(@PathVariable("categoryId") Long categoryId) {
        List<Map<String, Object>> list = sysDicService.selectSysDicList(categoryId);

        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:category:child:query')")
    @GetMapping("/child/{cid}")
    public AjaxResult childInfo(@PathVariable Long cid) {
        return AjaxResult.success(sysDicService.selectSysDicById(cid));
    }

    /**
     * 添加子分类
     * @param sysDic
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:category:child:add')")
    @Log(title = "数据字典子级分类", businessType = BusinessType.INSERT)
    @PostMapping(value = "/child")
    public AjaxResult addChild(@RequestBody SysDic sysDic) {
        return toAjax(sysDicService.insertSysDic(sysDic));
    }

    @PreAuthorize("@ss.hasPermi('system:category:child:edit')")
    @Log(title = "数据字典子级分类", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/child")
    public AjaxResult editChild(@RequestBody SysDic sysDic) {
        System.out.println("sysDic = " + sysDic);
        return toAjax(sysDicService.updateSysDic(sysDic));
    }

    /**
     * 删除子分类
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:category:child:remove')")
    @DeleteMapping("/child/delChild/{id}")
    public AjaxResult delChild(@PathVariable Long id) {
        if (sysDicService.selectSysDicByPid(id)) {
            return AjaxResult.error("请先删除子分类");
        }

        return AjaxResult.success(sysDicService.deleteSysDicById(id));
    }
}
